import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import HomeView from "../views/home/index.vue";

import Layout from '../views/layout/index.vue'

const config = import.meta.env;
const router = createRouter({
  history: createWebHistory(config.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: Layout,
      redirect:'/home',
      children:[
        {
          path:'/home',
          name:'首页',
          component:HomeView,
        },
        {
          path: "/demo",
          name: "demo",
          component: () => import("../views/demo.vue"),
        },
      ]
    },
    {
      path: "/about",
      name: "about",
      // component: () => import("../views/AboutView.vue"),
      component: () => import("../views/ContractView/index.vue"),
    },
    {
      path: "/orderQuery",
      name: "orderQuery",
      meta: {
        access: true,
      },
      component: () => import("../views/OrderQuery/index.vue"),
    },
    {
      path: "/orderDetails",
      name: "orderDetails",
      meta: {
        access: true,
      },
      component: () => import("../views/OrderDetails/index.vue"),
    },
    /* 下方补充 */
    {
      path: "/pay",
      name: "pay",
      meta: {
        access: true,
      },
      component: () => import("../views/PayView.vue"),
    },
    {
      path: "/signature",
      name: "signature",
      meta: {
        access: true,
      },
      component: () => import("../views/Signature.vue"),
      // component: () => import("../views/SignaturePad.vue"),
    },
    {
      path: "/orderSignature",
      name: "orderSignature",
      meta: {
        access: true,
      },
      component: () => import("../views/OrderSignature.vue"),
    },
    {
      path: "/contractInfo",
      name: "contractInfo",
      meta: {
        access: true,
      },
      component: () => import("../views/ContractInfo.vue"),
    },
    {
      path: "/contractEditing", // 合同编辑
      name: "contractEditing",
      meta: {
        access: true,
      },
      component: () => import("../views/ContractEditing.vue"),
    },
    /* 上方补充 */
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/LoginView.vue"),
    },
    
  ],
});
//前置守卫
router.beforeEach((to, from, next) => {
  // document.title = config.VITE_TITLE;
  const token = sessionStorage.getItem("token");
  let access = to.meta.access;
  if (access) {
    next();
  } else {
    if (!token && to.path !== "/login") {
      next({name: 'login'})
    } else {
      next();
    }
  }
});

//后置钩子
router.afterEach((to, from) => {
  // console.log(to, from);
});
export default router;
